/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package calculator;

import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import javax.swing.*;

/**
 *
 * @author CJ
 */
public class Calculator extends JPanel implements KeyListener{

    protected JLabel[] text;
    protected JLabel[] backup;
    protected boolean flag = false;
    protected ArrayList runoff;

    public Calculator()
    {
        setPreferredSize(new Dimension(500, 500));
        setFocusable(true);
        addKeyListener(this);

        setLayout(new GridLayout(10, 1));

        runoff = new ArrayList();

        initDisplay();
    }

    public void initDisplay()
    {
        text = new JLabel[10];
        for(int i = 0;i < text.length; ++i)
        {
            text[i] = new JLabel(i + ":");
            add(text[i]);
        }
        backup();
    }

    public void keyPressed(KeyEvent e)
    {
        int first = 1;
        int second = 2;
        int pressed = e.getKeyCode();
        if(pressed == 16)
            flag = true;
        boolean empty = !text[1].getText().equals("1:")
                && !text[2].getText().equals("2:");
        boolean special = !text[0].getText().equals("0:");
        if(special)
        {
            first = 0;
            second = 1;
            empty = true;
        }
        if(pressed < 58 && pressed > 47 && !flag)
        {
            pressed -= 48;
            text[0].setText(text[0].getText() + ((Integer)pressed).toString());
        }
        else if(pressed == KeyEvent.VK_U)
        {
            restore();
        }
        else
        {
            backup();
            if(pressed == KeyEvent.VK_MINUS && empty)
                {
                Double one = Double.parseDouble(text[first].getText().substring(2));
                Double two = Double.parseDouble(text[second].getText().substring(2));
                Double sum = two - one;
                text[second].setText(second + ":" + sum.toString());
                if(!special)
                    shiftUp();
                text[0].setText("0:");
            }
            else if(pressed == 61 && empty)
            {
                Double one = Double.parseDouble(text[first].getText().substring(2));
                Double two = Double.parseDouble(text[second].getText().substring(2));
                Double sum = one + two;
                text[second].setText(second + ":" + sum.toString());
                if(!special)
                    shiftUp();
                text[0].setText("0:");
            }
            else if(pressed == 56 && empty)
            {
                Double one = Double.parseDouble(text[first].getText().substring(2));
                Double two = Double.parseDouble(text[second].getText().substring(2));
                Double sum = one * two;
                text[second].setText(second + ":" + sum.toString());
                if(!special)
                    shiftUp();
                text[0].setText("0:");
            }
            else if(pressed == KeyEvent.VK_SLASH && empty)
            {
                Double one = Double.parseDouble(text[first].getText().substring(2));
                Double two = Double.parseDouble(text[second].getText().substring(2));
                Double sum = (double)two / one;
                text[second].setText(second + ":" + sum.toString());
                if(!special)
                    shiftUp();
                text[0].setText("0:");
            }
            else if(pressed == KeyEvent.VK_ENTER)
            {
                shiftDown();
            }
            else if(pressed == KeyEvent.VK_BACK_SPACE)
            {
                if(!text[0].getText().equals("0:"))
                    text[0].setText(text[0].getText().substring(0, text[0].getText().length() - 1));
            }
        }
    }

    public void keyTyped(KeyEvent e)
    {

    }

    public void keyReleased(KeyEvent e)
    {
        if(e.getKeyCode() == 16)
            flag = false;
    }

    public void shiftDown()
    {
        if(text[0].getText().equals("0:"))
            return;

        runoff.add(text[text.length-1].getText().substring(2));

        for(int i = text.length - 1; i > 0; --i)
        {
            text[i].setText(i + ":" + text[i - 1].getText().substring(2));
        }
        text[0].setText("0:");
    }

    public void shiftUp()
    {
        for(int i = 1; i < text.length - 1; ++i)
        {
            text[i].setText(i + ":" + text[i+1].getText().substring(2));
        }
        text[text.length - 1].setText(text.length - 1 + ":" + runoff.get(runoff.size() - 1));
        runoff.remove(runoff.size() - 1);
    }

    public void backup()
    {
        if(backup == null)
        {
            backup = new JLabel[text.length];
            for(int i = 0;i < backup.length;++i)
                backup[i] = new JLabel();
        }
        for(int i = 0;i< text.length;++i)
        {
            String a = text[i].getText();
            backup[i].setText(a);
        }
    }

    public void restore()
    {
        for(int i = 0;i < text.length;++i)
            text[i].setText(backup[i].getText());
    }
}
